﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using System.Threading;
using ISS.IR.RRNMS.Library.BusinessLogic;

namespace WebApplication1
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CultureInfo newCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
            newCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";
            newCulture.DateTimeFormat.DateSeparator = "-";
            Thread.CurrentThread.CurrentCulture = newCulture;

            HttpCookie cookie = new HttpCookie("A", "FirstTime");
            cookie.Expires = DateTime.Now.Add(new TimeSpan(0, 0, 15));
            Response.Cookies.Add(cookie);

            if (Session["UserId"] != null && Guid.Parse(Session["UserId"].ToString()) != Guid.Empty)
            {
                programmaticModalPopup_Login.Hide();

                Guid UserId = Guid.Parse(Session["UserId"].ToString());

                UserPermissions Permissions = new UserPermissions();

                if (Session["UserPermissions"] != null)
                    Permissions = (UserPermissions)Session["UserPermissions"];
                else
                    Permissions = IR_User.SelectUserPermissions(UserId);

                if (!Permissions.IsAdmin) NavigationMenu.Items.RemoveAt(3);
                if (!Permissions.HasSearchPerm) NavigationMenu.Items.RemoveAt(2);
                if (!Permissions.HasAccountPerm) NavigationMenu.Items.RemoveAt(1);
                if (!Permissions.HasCalendarPerm) NavigationMenu.Items.RemoveAt(0);
            }
            else
                programmaticModalPopup_Login.Show();


        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtUserName.Text) && !String.IsNullOrEmpty(txtPassword.Text))
            {
                Guid UserId = IR_User.Login(txtUserName.Text, txtPassword.Text);
                if (UserId != Guid.Empty)
                {
                    Session["UserId"] = UserId;
                    UserPermissions Permissions = IR_User.SelectUserPermissions(UserId);
                    Session["UserPermissions"] = Permissions;

                    if (!Permissions.IsAdmin) NavigationMenu.Items.RemoveAt(3);
                    if (!Permissions.HasSearchPerm) NavigationMenu.Items.RemoveAt(2);
                    if (!Permissions.HasAccountPerm) NavigationMenu.Items.RemoveAt(1);
                    if (!Permissions.HasCalendarPerm) NavigationMenu.Items.RemoveAt(0);

                    programmaticModalPopup_Login.Hide();
                }
            }
        }

        protected void btnLogOut_Click(object sender, EventArgs e)
        {
            try
            {
                Session.RemoveAll();

                Response.Cache.SetCacheability(HttpCacheability.NoCache);
                Response.Cache.SetExpires(DateTime.Now);

                Page_Load(null,null);

            }
            catch (Exception exc)
            { 
            
            }
        }
    }
}
